<template>
  <div id="container">
    <div id="registerWarp">
      <h1>register</h1>
      <el-form :model="ruleForm" :label-position="labelPosition" :rules="rules" ref="ruleForm" label-width="100px"
        class="ruleForm">
        <el-form-item label="账号" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off" placeholder="请输入账号">账号</el-input>
        </el-form-item>
        <el-form-item label="公司名称" prop="company">
          <el-input type="text" v-model="ruleForm.company" autocomplete="off" placeholder="请输入公司名称">公司</el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="请再次输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.prevent="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import NavMenu from "@/components/NavMenu";
import ajax from "@/components/ajax";
import md5 from "js-md5";

export default {
  name: "register",
  data() {
    var checkName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        if (this.ruleForm.checkPass !== "") {
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        pass: "",
        checkPass: "",
        company: ""
      },
      labelPosition: "right",
      rules: {
        username: [
          {
            validator: checkName,
            trigger: "blur"
          },
          { min: 6, max: 12, message: "用户名长度为 6-12 位", trigger: "blur" }
        ],
        pass: [
          {
            validator: validatePass,
            trigger: "blur"
          },
          { min: 6, max: 12, message: "密码长度应为 6-12 位", trigger: "blur" }
        ],
        checkPass: [
          {
            validator: validatePass2,
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      var that = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          const data = this.$refs[formName].model;

          let formData = new FormData();
          formData.append("username", data.username);
          formData.append("company", data.company);
          formData.append("pass", md5(data.pass));

          ajax({
            method: "post",
            url: "http://127.0.0.1:3000/register",
            data: formData,
            withCredentials: true,
            success: function(res) {
              if (res.status === 0) {
                that.$message({
                  message: "注册成功",
                  type: "success"
                });
                that.$router.push({
                  path: "/login"
                });
              } else {
                that.$message.error({
                  message: res.data.msg
                });
              }
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style>
#container {
  display: flex;
  justify-content: center;
}

@media screen and (max-width: 500px) {
  #registerWarp {
    width: 85vw;
  }

  .el-form-item {
    width: 80vw;
    padding-bottom: 1rem;
  }
}

@media screen and (min-width: 501px) {
  #registerWarp {
    /* width: 25vw; */
  }

  .el-input {
    width: 20vw;
    padding-bottom: 1rem;
  }
}
</style>
